﻿using DLLPC.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace VisionCore
{
    public class Status_SortFinalALG : ProcedureHelper_Exc
    {
        string subStatusCode = "";
        string tempStatusCode = "";
        bool condition = false;
        bool start = false;

        SystemTimeCounter scStatus = new SystemTimeCounter();
        public void Main()
        {
            condition = /*SortST_Presence &&*/ Kernel.motion.LDTR_R.IsHomeAlready && Kernel.motion.SortST_X.IsHomeAlready && Kernel.motion.SortST_Y.IsHomeAlready && Kernel.motion.SortST_R.IsHomeAlready && Kernel.motion.CCD_1.IsHomeAlready && !SortSTFinalAlignPass && !DryRunMode;
            start = condition && VisionAutoMode &&LDTRStatusCode == LDTRStatus.WaitAlign && SortStatusCode == SortStatus.AlignReq && !SortSTFinalAlignOK;

            switch (SortFinalALGStatusCode)
            {
                case AlignStatus.Init:
                    subStatusCode = "00: 准备";
                    {
                        if (start)
                        {
                            SortFinalALGStatusCode = AlignStatus.AtAlignPos;
                        }
                    }
                    break;
                case AlignStatus.AtAlignPos:
                    subStatusCode = "01: LDTR_R及SortST到对位位";
                    {
                        if (LDTR_R_ALG && LDTR_CCD_ALG && SortST_X_Sort && SortST_Y_Sort && SortST_R_Sort)
                        {
                            SortSTFinalAlignNGPass = false;
                            SortFinalALGStatusCode = AlignStatus.Trigger;
                            scStatus.Start();
                        }
                    }
                    break;
                case AlignStatus.Trigger:
                    subStatusCode = "02: 对位触发";
                    {

                        if (scStatus.Duration > 100 && RTCHelper.IOEvent.HSD_SortSTFinalAlignmentReportTrigger.ReplyBitValue == 0 && RTCHelper.IOEvent.HSD_SortSTFinalAlignmentReportTrigger.TrigBitValue == 0)//相机对位触发
                        {
                            RTCHelper.IOEvent.HSG_SortSTFinalAlignmentTrigger.Trig();
                            SortFinalALGStatusCode = AlignStatus.TriggerComplete;
                            scStatus.Start();
                        }
                    }
                    break;
                case AlignStatus.TriggerComplete:
                    subStatusCode = "03: 对位完成";
                    {
                        if (!(SortSTFinalAlignOK || (!SortSTFinalAlignOK && SortSTFinalAlignNGPass)) && scStatus.Duration > 5000)
                        {
                            SortFinalALGStatusCode = AlignStatus.Trigger;
                        }
                        if (SortSTFinalAlignOK||(!SortSTFinalAlignOK && SortSTFinalAlignNGPass))
                        {
                            SortFinalALGStatusCode = AlignStatus.Init;
                        }
                    }
                    break;
                default:
                    break;
            }

            LogStatusCode(subStatusCode);
        }

        private void LogStatusCode(string msg)
        {
            if (msg != tempStatusCode)
            {
                tempStatusCode = msg;
                LogMsg("挑拣精定位- " + tempStatusCode);
            }
        }
    }
}
